import type { TableColumn } from '@/framework/components/core/super-table';
import { commonCode } from '@/api/common/common_code';
import { ref } from 'vue';

export const enumData = ref();

/**
 * columns
 */
export type TableListItem = {
  id;
  username;
  name;
  roleList;
  identityCode;
  status;
};
export type TableColumnItem = TableColumn<TableListItem>;

export const baseColumns: TableColumnItem[] = [
  {
    type: 'checkbox',
    title: '',
    width: 60,
  },
  {
    type: 'seq',
    title: '序号',
    width: 60,
  },
  {
    title: '用户名',
    field: 'username',
  },
  {
    title: '姓名',
    field: 'name',
  },
  {
    title: '角色',
    field: 'roleList',
    formatter: ({ cellValue }) => {
      if (!cellValue) return '';
      const newArr: any[] = Object.values(cellValue).reduce((pre: any[], cur: any[]) => {
        pre.push(...cur);
        return pre;
      }, []);
      return newArr?.map((item) => item.name)?.join() ?? '';
    },
    hideInSearch: true,
  },
  {
    title: '身份卡',
    field: 'identityCode',
    hideInSearch: true,
  },
  {
    title: '状态（是否启用）',
    field: 'status',
    formatter: ({ cellValue }) => {
      return enumData.value?.find((item) => item?.value === String(cellValue))?.label ?? '';
    },
    formItemProps: {
      component: 'Select',
      componentProps: {
        request: async () => {
          const res = await commonCode('TrueOrFalse');
          return (enumData.value = res.result.map((item) => ({
            label: item.name,
            value: item.code,
          })));
        },
      },
      labelWidth: 150,
    },
  },
];
